package com.illya.controller;

import com.illya.exciption.IllyaExciption;
import com.illya.pojo.UserAccount;
import com.illya.result.Result;
import com.illya.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/illya")
public class AccountController {
    @Autowired
    private AccountService service;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/altria")
    public Result cost(@RequestBody UserAccount account, String keys) {
        if (account == null) {
            throw new IllyaExciption("有病吧你~");
        }
        Long redis = redisTemplate.opsForValue().increment(keys);
        if (redis > 1) {
            return Result.error("请勿重复操作~");
        }
        service.cost(account);
        return Result.success("扣款成功~");
    }
}
